/**
 * Created by alienity on 2016/1/14.
 */
var GameLayer = cc.Layer.extend({
    _grid: [],
    _size: null,
    _stepLabel: 0,
    _levelLabel: null,
    ctor: function () {
        this._super();
        this.init();
    },
    init: function () {
        this.initController();
        this.initConfig();
        this.initMap();
        this.initLabel();
    },
    initController: function () {
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan.bind(this),
        }, this);
    },
    initConfig: function () {
        this._size = cc.winSize;
        config.width = this._size.width - 50;
    },
    initMap: function () {
        for(var i = 0; i < config.level; ++i){
            this._grid[i] = [];
            for(var j = 0; j < config.level; ++j){
                var lampSize = this.calculateLampSize();
                var increment = lampSize + config.distance;
                var x = this._size.width/2-config.width/2;
                var y = this._size.height/2-config.width/2;
                var lamp = new Lamp(lampSize);
                lamp.setPosition(x + increment * (i + 0.5), y + increment * (j + 0.5));
                this.addChild(lamp);
                this._grid[i][j] = lamp;
            }
        }
    },
    initLabel: function () {
        Counter.resetStep();
        this._stepLabel = new cc.LabelTTF("Step  " + Counter.step, "Arial", 24);
        this.addChild(this._stepLabel);
        this._stepLabel.setPosition(this._size.width - 60, this._size.height - 50);
        this._levelLabel = new cc.LabelTTF("Level  " + config.level, "Arial", 28);
        this.addChild(this._levelLabel);
        this._levelLabel.setPosition(60, this._size.height - 50);
    },
    checkTouchLamp: function (location) {
        var lampSize = this.calculateLampSize();
        for(var i = 0; i < this._grid.length; ++i){
            for(var j = 0; j < this._grid[i].length; ++j){
                var lamp = this._grid[i][j];
                var pos = lamp.getPosition();
                var wl = Math.abs(pos.x - location.x);
                var hl = Math.abs(pos.y - location.y);
                if(wl < lampSize/2 && hl < lampSize/2)
                    return cc.p(i, j);
            }
        }
    },
    updateLamp: function (pos) {
        if(pos.x-1 >= 0)
            this._grid[pos.x-1][pos.y].turn();
        if(pos.x+1 < config.level)
            this._grid[pos.x+1][pos.y].turn();
        if(pos.y-1 >= 0)
            this._grid[pos.x][pos.y-1].turn();
        if(pos.y+1 < config.level)
            this._grid[pos.x][pos.y+1].turn();
        this._grid[pos.x][pos.y].turn();
        this.updateStep();
        if(this.checkGameWin())
            this.updateLevel();
    },
    updateStep: function () {
        Counter.step++;
        this._stepLabel.setString("Step  " + Counter.step);
    },
    updateLevel: function () {
        config.level++;
        cc.director.runScene(new GameScene());
    },
    checkGameWin: function () {
        for(var i = 0; i < this._grid.length; ++i){
            for(var j = 0; j < this._grid[i].length; ++j){
                var lamp = this._grid[i][j];
                if(lamp._color == 0)
                    return false;
            }
        }
        return true;
    },
    calculateLampSize: function () {
        return config.width/config.level - config.distance;
    },
    onTouchBegan: function (touch, event) {
        var pos = this.checkTouchLamp(touch.getLocation());
        if(pos != undefined)
            this.updateLamp(pos);
    },
});

var GameScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new GameLayer();
        this.addChild(layer);
    }
});
